<?php
/**
 * @CopyRight  (C)2006-2011 LiangJing Development team Inc.
 * @WebSite    www.liangjing.org www.asp99.cn
 * @Author     Liangjing.org <asp3721@hotmail.com>
 * @Brief      liangjingcms v1.x
 * @Update     2012.09.15
**/
if(!defined('PHP_KINGFISHER')) {
	exit('Access Denied');
}
class chency_mysql {
	var $querynum = 0;
	var $link;
	var $charset;
	function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = 'utf8', $pconnect = 0, $halt = TRUE) {
		if($pconnect) {
			if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
				$halt && $this->halt('Can not connect to MySQL server');
			}
		} else {
			if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw, 1)) {
				$halt && $this->halt('Can not connect to MySQL server');
			}
		}
		if($this->version() > '4.1') {
			if($dbcharset) {
				@mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->link);
			}
			if($this->version() > '5.0.1') {
				@mysql_query("SET sql_mode=''", $this->link);
			}
		}
		if($dbname) {
			@mysql_select_db($dbname, $this->link);
		}
	}   
	function select_db($dbname) {
		return mysql_select_db($dbname, $this->link);
	}
	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}
	function fetch_first($sql) {
		return $this->fetch_array($this->query($sql));
	}
	function result_first($sql) {
		return $this->result($this->query($sql), 0);
	}
	function query($sql, $type = '') {
		$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
			'mysql_unbuffered_query' : 'mysql_query';
		if(!($query = $func($sql, $this->link))) {
			if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
				$this->close();
				require CHENCY_ROOT.'./libs/db.inc.php';
				$this->connect(DB_HOST, DB_USER, DB_PASS, DB_DATA, DB_CHARSET, DB_PCONNECT,true);
				return $this->query($sql, 'RETRY'.$type);
			} elseif($type != 'SILENT' && substr($type, 5) != 'SILENT') {
				$this->halt('MySQL Query Error', $sql);
			}
		}
		$this->querynum++;
		return $query;
	}
	function affected_rows() {
		return mysql_affected_rows($this->link);
	}
	function fetch_assoc($query) {
		return mysql_fetch_assoc($query);
	}
	function error() {
		return (($this->link) ? mysql_error($this->link) : mysql_error());
	}
	function errno() {
		return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
	}
	function result($query, $row = 0) {
		$query = @mysql_result($query, $row);
		return $query;
	}
	function num_rows($query) {
		$query = mysql_num_rows($query);
		return $query;
	}
	function getRow($sql, $limited = false) {
		if ($limited == true) {
			$sql = trim($sql . ' LIMIT 1');
		}
		$res = $this->query($sql);
		if ($res !== false) {
			return mysql_fetch_assoc($res);
		} else {
			return false;
		}
	}
	function num_fields($query) {
		return mysql_num_fields($query);
	}
	function free_result($query) {
		return mysql_free_result($query);
	}
	function insert_id() {
		return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}
	function fetch_row($query) {
		$query = mysql_fetch_row($query);
		return $query;
	}
	function fetch_fields($query) {
		return mysql_fetch_field($query);
	}
	function version() {
		if(empty($this->version)) {
			$this->version = mysql_get_server_info($this->link);
		}
		return $this->version;
	}
	function close() {
		return mysql_close($this->link);
	}
	function halt($message = '', $sql = '') {
		$dberror = $this->error();
		$dberrno = $this->errno();
		echo "<meta http-equiv='Content-Type' content='text/html;charset=".LJCMS_CHARSET."' /><div style=\"position:absolute;font-size:11px;font-family:verdana,arial;background:#EBEBEB;padding:0.5em;\"><b>MySQL Error</b><br><b>Message</b>: $message<br><b>SQL</b>: $sql<br><b>Error</b>: $dberror<br><b>Errno.</b>: $dberrno<br></div>";
		exit();
	}
    function getall($sql,$iscache=true){
		$res = $this->query($sql);
		if ($res !== false){
			$arr = array();
			while ($row = mysql_fetch_assoc($res)){
				$arr[] = $row;
			}
		}else{
			return false;
		}
		return $arr;
    }
    function insert($table,$array,$is=0) {
		$fields = $values = array();
		$table=str_replace('%s',DB_PRE,$table);
		if(empty($array)) return false;
		$sql_array=array();
		if($is) {
			foreach ($array[0] as $key => $val) {
			    $fields[]=$key;
			}
			foreach ($array as $arr) {
				foreach ($arr as $key => $val) {
				    $values[]=$this->encode($val);
				}
			    $sql_array[]='(\''.implode('\',\'',$values).'\')';
			    $values=array();
			}
		}else {
			foreach($array as $key=>$val) {
				$fields[]=$key;
				$values[]=$this->encode($val);
			}
			$sql_array[]='(\''.implode('\',\'',$values).'\')';
		}
		$sql = 'INSERT INTO '.$table.' ('.implode(',',$fields).') VALUES ';
		$sql.= implode(',',$sql_array).';';

		if($this->query($sql)){
			return true;
		}else{
			return false;
		}
    }
    function update($table,$array,$where=null) {
		$fields = $values = array();
		foreach($array as $key=>$val) {
			$values[]=preg_match("/^\[\[.+\]\]$/",$val)
			? $key.'='.substr($val,2,-2)
			: $key.'=\''.$this->encode($val).'\'';
		}
		$sql='UPDATE '.$table.' SET '.implode(',',$values).(empty($where) ?  ';' : ' WHERE '.$where.';');
		if($this->query($sql)){
			return true;
		}else{
			return false;
		}
    }
    function encode($s) {
	    return addslashes($s);
    }
    function fetch_newid($sql,$startid){
	    $n_id   = 0;
		$result = $this->query($sql);
		$ns     = $this->result($result,0);
		if(empty($ns) || $ns==""){
			$n_id = $startid;
		}else{
			$n_id = ($ns+1);
		}
	    return $n_id;
    }
	function fetch_count($sql){
		return $this->result($this->query($sql),0);
	}
	function checkdata($sql){
		$returnflag  = false;
		$result = $this->query($sql);
		if($this->num_rows($result)==0){
			$returnflag = false;
		}else{
			$returnflag = true;
		}
		return $returnflag;
	}
	function get_one($SQL){
		$query=$this->query($SQL,'UNBUFFERED');
		$rs =& mysql_fetch_array($query, MYSQL_ASSOC);
		return $rs;
	}
}
?>